// 区分开发环境和生产环境  接口地址
let baseUrl;

if (process.env.NODE_ENV === 'development') {
    baseUrl = 'https://www.1cardd.com/heka/app'    // 开发环境
} else {
    baseUrl = 'https://www.1cardd.com/heka/app'
    // baseUrl = 'http://47.110.48.22/heka/test'    // 生产环境
}
export const url = baseUrl
const whiteList = [
    '/api/v1/index/banners',
    '/api/v1/index/card-themes',
    '/api/v1/cards/themes'
]

// 同步wx.login
export const wxLogin = () => {
    return new Promise((reslove) => {
        wx.login({
            success(res) {
                reslove(res.code);
            }
        })
    })
}
const https = async function (config) {
    //显示loading
    wx.showLoading({
        title: '加载中...'
    });
    //检查缓存中有没有token
    if (!wx.getStorageSync('token')) {
        // 登陆获取code
        const code = await wxLogin();
        // code换取token
        const token =  await new Promise((resolve, reject) => {
            uni.request({
                url: baseUrl + '/api/v1/auth/token',
                method: 'POST',
                header: {
                    'X-UNI-AUTHCODE': code,
                }
            }).then(res => {
                wx.hideLoading()
                if (res[0]) {
                    uni.showToast({
                        title: "登陆失败",
                        icon: "none",
                        duration: 3000
                    })
                    resolve(false);
                } else {
                    let data = res[1].data;
                    if (data.code === 200) {
                        resolve(data.data.token);
                    } else {
                        uni.showToast({
                            title: data.msg,
                            icon: "none",
                            duration: 3000
                        })
                        reject()
                    }
                }
            }).catch(error => {
                wx.hideLoading()      //隐藏loading
                reject(error);
            })
        })
        wx.setStorageSync('token', token)
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request({
            url: baseUrl + config.url,
            method: config.method || 'GET',
            data: config.data || {},
            header: {
                'X-UNI-TOKEN': uni.getStorageSync("token"),
                'content-type': 'application/json',
            }
        }).then(res => {
            wx.hideLoading()      //隐藏loading
            if (res[0]) {
                uni.showToast({
                    title: "数据获取失败",
                    icon: "none",
                    duration: 3000
                })
                resolve(false);
            } else {
                let data = res[1].data;
                if (data.code === 10004 || data.code === 401 || data.msg === 'token已失效或为非法值') {
                    uni.redirectTo({
                        url: '/pages/login/index'
                    })
                    reject()
                    return
                }
                if (data.code === 200) {
                    resolve(data);
                } else {
                    uni.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 3000
                    })
                    reject()
                }
            }
        }).catch(error => {
            wx.hideLoading()      //隐藏loading
            reject(error);
        })
    })
    return promise
}

export default https
